<template>
	<view class="u-wrap">
		<view>
			<u-tabs :list="list1" active-color="#15BBA3" :current="tab1_current" @change="set_tab1"></u-tabs>
		</view>
		<view class="u-menu-wrap">
			<scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view" :scroll-top="scrollTop">
				<view v-for="(item,index) in list2" :key="index" class="u-tab-item" :class="[current==index ? 'u-tab-item-active' : '']"
				 :data-current="index" @tap.stop="swichMenu(index)">
					<text class="u-line-1">{{item.name}}</text>
				</view>
				<view class="u-tab-item">
					<text class="u-line-1">
						<u-icon name="plus-circle" size="38" @click="show_class_info_popup({pid:list1[tab1_current].id,type:1})"></u-icon>
					</text>
				</view>
			</scroll-view>
			<block v-for="(item,index) in list2" :key="index">
				<scroll-view scroll-y class="right-box" v-if="current==index">
					<view class="page-view">
						<view class="class-item">
							<u-collapse
							    @change="change"
							    @close="close"
							    @open="open"
							  >
							    <u-collapse-item
									:title="item1.name"
									:open="true"
									name="Docs guide"
									class="item-title"
									v-for="(item1, index1) in item.children" :key="index1" 
							    >
									<view class="u-collapse-content item-container">
										<view class="thumb-box" v-for="(item2, index2) in item1.children" :key="index2">
											<view v-if="page_type==2" class="item-menu-name" @click="addzd2yaofang(item2.id,item2.name)">{{item2.name}}</view>
											<view v-else class="item-menu-name" @click="show_class_info_popup(item2)">{{item2.name}}</view>
										</view>
										<view class="thumb-box">
											<u-icon name="plus-circle" size="38" @click="show_class_info_popup({pid:item1.id,type:1})"></u-icon>
										</view>
									</view>
							    </u-collapse-item>
								<view v-if="page_type==1" style="padding-top: 33rpx;text-align: center;border-top: 1px solid #ccc;">
									<u-icon name="plus-circle" size="38" @click="show_class_info_popup({pid:list2[index].id,type:2})"></u-icon>
								</view>
							</u-collapse>
						</view>
					</view>
				</scroll-view>
			</block>
		</view>
		<u-popup border-radius="10" v-model="popup.show"
			@close="class_info_init" 
			mode="bottom" 
			length="500" 
			:closeable="popup.closeable"
			:close-icon-pos="popup.closeIconPos"
		>
			<view style="height: 300rpx;padding-top: 100rpx">
				<u-input v-model="class_name_info.name" :border="true" class="name_box"/>
				<view class="btn_list">
					<u-button type="error">删除</u-button>
					<u-button class="save_btn" @click="save_class">保存</u-button>
				</view>
			</view>
		</u-popup>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import { getClassesList,saveClasses,saveZhenduan } from '@/util/api/api.js';
	
	export default {
		data() {
			return {
				tab1_current:0,
				index:0,
				index1:0,
				index2:0,
				scrollTop: 0, //tab标题的滚动条位置
				current: 0, // 预设当前项的值
				menuHeight: 0, // 左边菜单的高度
				menuItemHeight: 0, // 左边菜单item的高度
				popup:{
					show:false,
					closeable:true,
				},
				list1: [],
				list2: [],
				list3: [],
				page_type:1,//1管理病症2给药方添加病症
				class_name_info:{}
			}
		},
		onLoad(e) {
			this.yf_id = e.id
			if(e.page_type){
				//1管理病症2给药方添加病症
				this.page_type = e.page_type
				
			}
			this.page_init()
		},
		methods: {
			page_init(){
				let that = this
				getClassesList({type:"zy"}).then((res)=>{
					console.log(res.data)
					that.list1 = res.data
					that.list2 = that.list1[that.tab1_current].children
					that.list3 = that.list2[that.index].children
				})
			},
			getImg() {
				return Math.floor(Math.random() * 35);
			},
			// 点击左边的栏目切换
			async swichMenu(index) {
				if(index == this.current) return ;

				this.current = index;
				
				// this.tabbar = this.list1[index].children
				console.log(this.list2[index])
				this.list3 = this.list2[index].children
				
				// 如果为0，意味着尚未初始化
				if(this.menuHeight == 0 || this.menuItemHeight == 0) {
					await this.getElRect('menu-scroll-view', 'menuHeight');
					await this.getElRect('u-tab-item', 'menuItemHeight');
				}
				// 将菜单菜单活动item垂直居中
				this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
			},
			// 获取一个目标元素的高度
			getElRect(elClass, dataVal) {
				new Promise((resolve, reject) => {
					const query = uni.createSelectorQuery().in(this);
					query.select('.' + elClass).fields({size: true},res => {
						// 如果节点尚未生成，res值为null，循环调用执行
						if(!res) {
							setTimeout(() => {
								this.getElRect(elClass);
							}, 10);
							return;
						}
						this[dataVal] = res.height;
					}).exec();
				})
			},
			set_tab1(e){
				this.tab1_current = e
				console.log(this.list1[e].children)
				this.list2 = this.list1[e].children
				this.list3 = this.list2[0].children
			},
			show_class_info_popup(item){
				console.log(item)
				
				this.class_info_init()
				this.popup.show = true
				
				if(item.id){
					this.class_name_info.id = item.id
				}
				
				if(item.pid){
					this.class_name_info.pid = item.pid
				}
				
				if(item.name){
					this.class_name_info.name = item.name
				}
				
				if(item.type){
					this.class_name_info.type = item.type
				}
			},
			hide_add_class_popup(){
				this.popup.show = false
				this.class_info_init()
			},
			class_info_init(){
				this.class_name_info = {
					id:0,
					name:'',
					type:0,
					pid:0
				}
			},
			save_class(){
				let that = this
				let class_name_info = this.class_name_info
				let add_data = {}
				add_data.pid = this.class_name_info.pid
				add_data.name = this.class_name_info.name
				add_data.type = this.class_name_info.type
				add_data.tableType = 'zy'
				if(this.class_name_info.id>0){
					add_data.id = this.class_name_info.id
				}
				
				saveClasses(add_data).then((res)=>{
					if (res.code==200) {
						that.showToast(res.msg)
						
						that.page_init()
						that.hide_add_class_popup()
					} else{
						that.showToast(res.msg)
					}
				})
			},
			showToast(text) {
				this.$refs.uToast.show({
					title: text,
					type: 'success',
					// url: '/pages/user/index'
				})
			},
			addzd2yaofang(id,name){
				let data = {}
				data.id = id
				data.name = name
				
				debugger
				getApp().globalData.yf_guanlian_zd.unshift(data)
				
				uni.navigateBack({
					url: '../yaofang/yaofang?yfid='+id+'&name='+name
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.u-wrap {
		height: calc(100vh);
		/* #ifdef H5 */
		height: calc(100vh - var(--window-top));
		/* #endif */
		display: flex;
		flex-direction: column;
	}
	.u-search-box {
		padding: 18rpx 30rpx;
	}
	.u-menu-wrap {
		flex: 1;
		display: flex;
		overflow: hidden;
	}
	.u-search-inner {
		background-color: rgb(234, 234, 234);
		border-radius: 100rpx;
		display: flex;
		align-items: center;
		padding: 10rpx 16rpx;
	}
	.u-search-text {
		font-size: 26rpx;
		color: $u-tips-color;
		margin-left: 10rpx;
	}
	.u-tab-view {
		width: 250rpx;
		height: 100%;
	}
	.u-tab-item {
		height: 110rpx;
		background: #f6f6f6;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 26rpx;
		color: #444;
		font-weight: 400;
		line-height: 1;
	}
	.u-tab-item-active {
		position: relative;
		color: #000;
		font-size: 30rpx;
		font-weight: 600;
		background: #fff;
	}
	.u-tab-item-active::before {
		content: "";
		position: absolute;
		border-left: 4px solid #15BBA3;
		height: 32rpx;
		left: 0;
		top: 39rpx;
	}
	.u-tab-view {
		height: 100%;
	}
	.right-box {
		width: 500rpx;
		background-color: rgb(250, 250, 250);
	}
	.page-view {
		padding: 16rpx;
	}
	.class-item {
		margin-bottom: 30rpx;
		background-color: #fff;
		padding: 16rpx;
		border-radius: 8rpx;
	}
	.item-title {
		font-size: 26rpx;
		color: $u-main-color;
		font-weight: bold;
	}
	.item-menu-name {
		font-weight: normal;
		font-size: 24rpx;
		color: $u-main-color;
	}
	.item-container {
		display: flex;
		flex-wrap: wrap;
	}
	.thumb-box {
		width: 208rpx;
		height: 56rpx;
		line-height: 56rpx;
		background: #F5F5F5;
		border-radius: 28rpx;
		margin: 5rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
		margin-top: 20rpx;
	}
	.item-menu-image {
		width: 120rpx;
		height: 120rpx;
	}
	.name_box{
		width: 650rpx;
		margin: 0 50rpx;
		
		
		.del_btn{
			width: 250rpx;
			margin: 50rpx;
		}
	}
	.btn_list{
		width: 700rpx;
		margin: 0 25rpx;
		margin-top: 140rpx;
		display: flex;
		
		.u-btn{
			width: 300rpx;
			height: 88rpx;
			border-radius: 44px;
			color: #FFFFFF;
		}
		.save_btn{
			background: #15BBA2;
		}
	}
</style>
